package com.atguigu.controller;

import com.atguigu.domain.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCollapser;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.utils.FallbackMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author 琉
 * @create 2021-08-12 10:32
 */
@RestController
@RequestMapping(value = "/consumer")
public class UserController {
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;

    @HystrixCommand(fallbackMethod="failBack")
    @GetMapping("/{id}")
    public User queryById(@PathVariable("id") Integer id){
//        String url = "http://localhost:18081/user/find/" + id;
//        return restTemplate.getForObject(url,User.class);

//        List<ServiceInstance> instances = discoveryClient.getInstances("user-provider");
//        ServiceInstance serviceInstance = instances.get(0);
//        String instanceUrl = "http://"+serviceInstance.getHost()+":"+serviceInstance.getPort()+"/user/find/"+id;
//        return restTemplate.getForObject(instanceUrl,User.class);
        String url = "http://user-provider/user/find/"+id;
        return restTemplate.getForObject(url,User.class);

    }

    public User failBack(Integer id){
        User user = new User();
        user.setUsername("服务降级!");
        return  user;
    }


}
